package cii_lib_graph.elements;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import resources.ButtonTabComponent;
import resources.DataCardChars;
import resources.DataDisplayable;
import testy.TestHardware;
import cii_lib.CCII_ACCESS_INTERF;

import componentEnhancement.CDataProperties;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DataCardPane extends javax.swing.JFrame {
	
	private static final Logger log = LoggerFactory
			.getLogger(DataCardPane.class);

//	{
//		//Set Look & Feel
//		try {
//			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//		} catch(Exception e) {
//			log.error("", e);
//		}
//	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 8440194099001829300L;
	private JSplitPane dispPanel;
	private ButtonGroup buttonGroupRowCol,buttonGroupRowCol2;
	private JRadioButton jRadioButtonColumn,jRadioButtonColumn2;
	private JTextField bfield;
	private JTextField afield;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JLabel jLabel5;
	private JTextField normSetText;
	private JPanel jPanel1;
	private JLabel jLabel4;
	private JPanel metricControlPanel;
	private JButton readAllButton;
	private JLabel jLabel3;
	private JButton jButton1;
	private JButton butStart;
	private JTextField jTextFieldOffs;
	private JLabel jLabel2;
	private JPanel jPanel2;
	private JButton jButtonAct;
	private JTabbedPane jTabbedPane;
	private JPanel dataPanel;
	private JLabel jLabel1;
	private JComboBox opsComboBox, opsComboBox2;
	private JPanel configPanel;
	private JRadioButton jRadioButtonRow,jRadioButtonRow2;
	private DataCardChars datapanelc;
	/**
	 * Data representation.
	 */
	private Object dataStore = null;
	private CDataProperties charProp = new CDataProperties();
	private CCII_ACCESS_INTERF intrer;
	
	private boolean isNormalize = false;
	private boolean isScale = false;
	private boolean isSimple = true;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			log.error("", e);
		} catch (InstantiationException e) { 
			log.error("", e);
		} catch (IllegalAccessException e) {
			log.error("", e);
		} catch (UnsupportedLookAndFeelException e) {
			log.error("", e);
		}
		DataCardPane inst = new DataCardPane();
		inst.setVisible(true);
	}
	public DataCardPane() {
		super();
		initGUI();
	}
	
	public DataCardPane(CCII_ACCESS_INTERF inter) {
		super();
		this.intrer=inter;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			{
				{
					buttonGroupRowCol = new ButtonGroup();
					buttonGroupRowCol2 = new ButtonGroup();
				}
				dispPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                        configPanel, dataPanel);
				getContentPane().add(dispPanel, BorderLayout.CENTER);
//				dispPanel.setLayout(null);
				dispPanel.setOneTouchExpandable(true);
				dispPanel.setDividerLocation(175);
//				dispPanel.
				{
					configPanel = new JPanel();
					dispPanel.add(configPanel);
					configPanel.setBounds(0, 0, 175, 382);
					configPanel.setLayout(null);
					configPanel.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
					{
						jRadioButtonRow = new JRadioButton();
						configPanel.add(jRadioButtonRow);
						jRadioButtonRow.setBounds(107, 42, 21, 14);
						buttonGroupRowCol.add(jRadioButtonRow);
					}
					{
						jRadioButtonColumn = new JRadioButton();
						configPanel.add(jRadioButtonColumn);
						jRadioButtonColumn.setBounds(138, 42, 21, 14);
						buttonGroupRowCol.add(jRadioButtonColumn);
					}
					{
						jRadioButtonRow2 = new JRadioButton();
						configPanel.add(jRadioButtonRow2);
						jRadioButtonRow2.setBounds(107, 65, 21, 14);
						buttonGroupRowCol2.add(jRadioButtonRow2);
					}
					{
						jRadioButtonColumn2 = new JRadioButton();
						configPanel.add(jRadioButtonColumn2);
						jRadioButtonColumn2.setBounds(138, 65, 21, 14);
						buttonGroupRowCol2.add(jRadioButtonColumn2);
					}
					{
						ComboBoxModel opsComboBoxModel = new DefaultComboBoxModel(
							new String[] { "! Not selected !", "Simple interface" , "Segment"});
						opsComboBox = new JComboBox();
						configPanel.add(opsComboBox);
						opsComboBox.setModel(opsComboBoxModel);
						opsComboBox.setBounds(2, 37, 103, 21);
						opsComboBox.setFont(new java.awt.Font("Tahoma",0,10));
						opsComboBox.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) { 
								log.info("Command: {}", opsComboBox.getSelectedItem());
								log.debug("opsComboBox.actionPerformed, event="
										+ opsComboBox.getSelectedItem());
								setCharProperties(opsComboBox.getSelectedItem().toString(),"");
								updateConfigPanel(opsComboBox.getSelectedItem().toString());
							}
						});
						opsComboBox.addItem("Normalize");
						opsComboBox.addItem("Scaled");
					}
					{

						ComboBoxModel opsComboBoxModel = new DefaultComboBoxModel(
							new String[] { "! Not selected !", "Simple interface" , "Time", "etc"});
						opsComboBox2 = new JComboBox();
						configPanel.add(opsComboBox2);
						opsComboBox2.setModel(opsComboBoxModel);
						opsComboBox2.setBounds(2, 62, 103, 21);
						opsComboBox2.setFont(new java.awt.Font("Tahoma",0,10));
						opsComboBox2.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) { 
								log.info("Command: {}", opsComboBox2.getSelectedItem());
								log.debug("opsComboBox.actionPerformed, event="
										+ opsComboBox.getSelectedItem());
							}
						});
					
					}
					{
						jLabel1 = new JLabel();
						configPanel.add(jLabel1);
						jLabel1.setText("Row   Column");
						jLabel1.setBounds(105, 21, 70, 21);
					}
					{
						jButtonAct = new JButton();
						configPanel.add(jButtonAct);
						jButtonAct.setText("Show data");
						jButtonAct.setBounds(12, 359, 84, 21);
						jButtonAct.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) { 
								log.info("Command: {} {} offset {} Metrics {} {} {} {}", 
										new Object[] {
										evt.getActionCommand(), 
										createNormLogData(), 
										jTextFieldOffs.getText(),
										opsComboBox.getSelectedItem(), jRadioButtonRow.isSelected() ? "Row" : jRadioButtonColumn.isSelected() ? "Col" : "",
										opsComboBox2.getSelectedItem(), jRadioButtonRow2.isSelected() ? "Row" : jRadioButtonColumn2.isSelected() ? "Col" : ""
								});
								showDataAct();									
							}
						});
					}
					{
						jLabel2 = new JLabel();
						configPanel.add(jLabel2);
						jLabel2.setText("Metrics");
						jLabel2.setBounds(14, 21, 70, 14);
					}
					{
						jTextFieldOffs = new JTextField();
						configPanel.add(jTextFieldOffs);
						jTextFieldOffs.setText("0");
						jTextFieldOffs.setBounds(56, 89, 105, 21);
					}
					{
						butStart = new JButton();
						configPanel.add(butStart);
						butStart.setText(">");
						butStart.setBounds(96, 359, 33, 21);
						butStart.setMargin(new java.awt.Insets(2, 2, 2, 2));
						butStart.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
								startReadingChart();
							}
						});
					}
					{
						jButton1 = new JButton();
						configPanel.add(jButton1);
						jButton1.setText("");
						jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
						jButton1.setBounds(131, 359, 35, 21);
						jButton1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
								stopReadingChart();
							}
						});
					}
					{
						jLabel3 = new JLabel();
						configPanel.add(jLabel3);
						jLabel3.setText("Offset:");
						jLabel3.setBounds(14, 89, 42, 21);
						jLabel3.setToolTipText("Address offset");
					}
					{
						readAllButton = new JButton();
						configPanel.add(readAllButton);
						readAllButton.setText("AllScope");
						readAllButton.setBounds(96, 336, 70, 21);
						readAllButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
						readAllButton.setToolTipText("Reads values from every registry address");
					}
					{
						metricControlPanel = new JPanel();
						configPanel.add(metricControlPanel);
						metricControlPanel.setBounds(2, 110, 170, 226);
						metricControlPanel.setBorder(BorderFactory.createTitledBorder(null, "Config:", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
						metricControlPanel.setLayout(null);
						
						{
							jPanel1 = new JPanel();
							metricControlPanel.add(jPanel1);
							jPanel1.setBounds(8, 17, 154, 204);
							jPanel1.setLayout(null);
							{
								jLabel4 = new JLabel();
								jPanel1.add(jLabel4);
								jLabel4.setText("- Nothing selected -");
								jLabel4.setBounds(30, 88, 94, 14);
							}

						}
						
					}
				}
				{
					dataPanel = new JPanel();
					BorderLayout jPanel1Layout = new BorderLayout();
					dispPanel.add(dataPanel);
					dataPanel.setLayout(jPanel1Layout);
					dataPanel.setBounds(175, 0, 431, 382);
					{
						jTabbedPane = new JTabbedPane();					
						dataPanel.add(jTabbedPane, BorderLayout.CENTER);
						jTabbedPane.setFont(new java.awt.Font("Tahoma",0,10));
						jTabbedPane
							.addContainerListener(new ContainerAdapter() {
							public void componentAdded(ContainerEvent evt) {
								log.debug("jTabbedPane.componentAdded, event="
										+ evt);
							}
							});
//						jTabbedPane.putClientProperty(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,
//								Boolean.TRUE);

					}
				}
			}

//			BigDecimal ts = new BigDecimal("940783947978230464563");
//			log.debug("big num: "+ts.doubleValue());
			
			pack();
			dispPanel.setSize(609,385);
			dispPanel.setPreferredSize(new java.awt.Dimension(602, 385));
			dispPanel.setEnabled(false);
			this.setSize(609, 424);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	public JSplitPane getDefaultPanel() {
		return dispPanel;
	}
	
	public JPanel getDataPanelChar(DataDisplayable displ) {
		return displ.getDefaultChartPanel();
	}
	
	protected void showDataAct() {
		datapanelc = new DataCardChars(intrer);
		jPanel2 = getDataPanelChar(datapanelc);
		dataStore= datapanelc.getDataStore();
		jTabbedPane.addTab("Data "+(jTabbedPane.getTabCount()), null, jPanel2, null);
		jTabbedPane.setTabComponentAt(jTabbedPane.getTabCount()-1, new ButtonTabComponent(jTabbedPane));
	}
	
	protected void startReadingChart() { 
		if (datapanelc==null) return;
		TestHardware.incrementType=2;
		datapanelc.getDataGenerator().getCollection().getSeries(0).clear();
		datapanelc.getDataGenerator().setStartPos(new Integer(jTextFieldOffs.getText()));
				   updateCharProperties();
		datapanelc.getDataGenerator().start("from panel ["+jTextFieldOffs.getText()+"...");
	}
	
	public void updateCharProperties() {
		if (isSimple) {
			setCharProperties("Simple", "");
		} else
		if (isNormalize) {
			try {
				Long tmp = new Long(normSetText.getText());
				setCharProperties("Normalize", ""+tmp);
			} catch (Exception e) {
				log.debug("Cant transform to number\n"+e);
				setCharProperties("Normalize", "");
			}	
		} else
		if(isScale) {
			
		} else {
			log.debug("segment ! ok");
			try {
				String get = normSetText.getText();
				setCharProperties("Segment", ""+get);
			} catch (Exception e) {
				log.debug("Cant transform to number\n"+e);
				setCharProperties("Normalize", "");
			}	
		}
		
	}
	
	public void setCharProperties(String name, String val) {
		if (datapanelc==null) return;
//		if (charProp.size()==0) charProp.setProperty(name,val);//TODO to jest do ewaluacji!
//		else {
////			charProp.remove(0);
			charProp.setProperty(name,val);
			log.debug("prop size: "+charProp.size());
		
		datapanelc.getDataGenerator().setProperties(charProp);
	}
	
	public void stopReadingChart() {
		if (datapanelc==null) return;
		datapanelc.getDataGenerator().stop();
	}
	
	public void updateConfigPanel(String name) {
		
		JPanel jCPanel = new JPanel();
		jCPanel.setBounds(8, 17, 154, 204);
		jCPanel.setLayout(null);

		if (name.equals("Segment")) {
			isNormalize=false;isScale=false; isSimple=false;
			{
				jLabel4 = new JLabel();
				jCPanel.add(jLabel4);
				jLabel4.setText("Define segment:");
				jLabel4.setBounds(10, 11, 96, 12);
			}
			{
				normSetText = new JTextField();
				jCPanel.add(normSetText);
				normSetText.setText("start:stop");
				normSetText.setBounds(10, 27, 134, 20);
			}
		} else
		if (name.equals("Normalize")) {
			isNormalize=true;isScale=false; isSimple=false;
			{
				jLabel4 = new JLabel();
				jCPanel.add(jLabel4);
				jLabel4.setText("Normalize with:");
				jLabel4.setBounds(10, 11, 96, 12);
			}
			{
				normSetText = new JTextField();
				jCPanel.add(normSetText);
				normSetText.setText("1");
				normSetText.setBounds(10, 27, 134, 20);
			}
		} else
		if (name.equals("Scaled")) {
			isNormalize=false;isScale=true; isSimple=false;
			{
				jLabel4 = new JLabel();
				jCPanel.add(jLabel4);
				jLabel4.setText("Scaling pattern:");
				jLabel4.setBounds(10, 11, 96, 12);
			}
			{
				jLabel5 = new JLabel();
				jCPanel.add(jLabel5);
				jLabel5.setText("y = ax + b");
				jLabel5.setBounds(10, 27, 98, 12);
				jLabel5.setFont(new java.awt.Font("Tahoma",1,11));
			}
			{
				jLabel6 = new JLabel();
				jCPanel.add(jLabel6);
				jLabel6.setText("a =");
				jLabel6.setBounds(10, 45, 24, 14);
			}
			{
				jLabel7 = new JLabel();
				jCPanel.add(jLabel7);
				jLabel7.setText("b =");
				jLabel7.setBounds(10, 64, 25, 11);
			}
			{
				afield = new JTextField();
				jCPanel.add(afield);
				afield.setText("1");
				afield.setBounds(33, 44, 109, 17);
				afield.setFont(new java.awt.Font("Tahoma",0,10));
			}
			{
				bfield = new JTextField();
				jCPanel.add(bfield);
				bfield.setText("0");
				bfield.setBounds(33, 62, 109, 17);
				bfield.setFont(new java.awt.Font("Tahoma",0,10));
			}
		} else {
			isNormalize=false;isScale=false; isSimple=true;
			{
				jLabel4 = new JLabel();
				jCPanel.add(jLabel4);
				jLabel4.setText("- Nothing selected -");
				jLabel4.setBounds(30, 88, 94, 14);
			}
		}
		
		metricControlPanel.remove(0);
		metricControlPanel.add(jCPanel);
		metricControlPanel.repaint();
	}
	
	/**
	* Auto-generated method for setting the popup menu for a component
	*/
	private void setComponentPopupMenu(
		final java.awt.Component parent,
		final javax.swing.JPopupMenu menu) {
		parent.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent e) {
				log.debug(e.toString());
				if (e.isPopupTrigger())
					menu.show(parent, e.getX(), e.getY());
			}
			public void mouseReleased(java.awt.event.MouseEvent e) {
				if (e.isPopupTrigger())
					menu.show(parent, e.getX(), e.getY());
			}
		});
	}
	
	private String createNormLogData() {
		if(opsComboBox.getSelectedIndex() == 0 || opsComboBox.getSelectedIndex() == 1) {
			return "";
		} else if(opsComboBox.getSelectedIndex() == 4) {
			return opsComboBox.getSelectedItem().toString() + " "+afield.getText()+" "+bfield.getText();
		}
		return opsComboBox.getSelectedItem().toString() + " "+normSetText.getText();
	}
}
